package com.gxa.hotel.sso.controller;


import com.gxa.hotel.core.common.JsonResult;
import com.gxa.hotel.core.exception.BusinessException;
import com.gxa.hotel.core.exception.ExceptionCode;
import com.gxa.hotel.sso.entity.User;
import com.gxa.hotel.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    UserService userService;

    @PostMapping
    public Map login(String phone,String code) throws Exception {
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException(ExceptionCode.EMPTY_PHONE);
        }
        if (StringUtils.isEmpty(code)){
            throw new BusinessException(ExceptionCode.EMPTY_CODE);
        }
        String token = userService.login(phone, code);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("登录成功");
        jsonResult.set("token",token);

        return jsonResult.getMap();
    }

    @PostMapping("/checkWithToken")
    public Map checkWithToken(String token) throws  Exception{
        User user = userService.checkWithToken(token);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(user);
        return jsonResult.getMap();
    }

}
